//package cn.jekun.shop.web.action;
//
//import java.sql.Types;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.annotation.Resource;
//
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;
//
//import org.apache.commons.lang.StringUtils;
//
//import cn.jekun.component.base.bean.ErrorMsg;
//import cn.jekun.component.base.exceptions.BusinessException;
//import cn.jekun.component.base.utils.EncryptUtil;
//import cn.jekun.component.base.utils.SqlUtis;
//import cn.jekun.component.core.Constants;
//import cn.jekun.component.core.bean.gen.RsUser;
//import cn.jekun.component.core.bean.gen.SgSettleApply;
//import cn.jekun.component.core.bean.gen.TsAccount;
//import cn.jekun.component.core.service.AccountPayService;
//import cn.jekun.shop.common.service.AccountService;
//import cn.jekun.shop.common.service.BanksService;
//import cn.jekun.shop.common.service.ShopBaseAction;
//import cn.jekun.shop.web.bean.BanksAccount;
//
///**
// * 提现
// * @author yuan
// *
// */
//public class WithdrawAction extends ShopBaseAction{
//	private static final String AUTH_SHOP_MY_ACCOUNT_SETTLE="SHOP_MY_ACCOUNT_SETTLE";//提现
//	
//	@Resource(name=AccountService.BEAN_NAME)
//	AccountService accountService;
//	
//	@Resource(name = BanksService.BEAN_NAME)
//	BanksService banksService;
//	
//	@Resource(name = AccountPayService.BEAN_NAME)
//	AccountPayService accountPayService;
//
//	/**
//	 * 提现
//	 * @return
//	 */
//	public String withdraw(){
//		
////		this.checkRight(AUTH_SHOP_MY_ACCOUNT_SETTLE);//检查权限
////		//获取账户余额
////		Map map = new HashMap();
////		map.put("shopId", this.getSessionUser().getShopId());
////		TsAccount tsAccount = this.accountService.one(this.accountService.getStatementName("query"),map );
////		if(tsAccount == null){
////			this.saveActionError("没有找到商家支付账户。");
////			return "/shop/withdraw.jsp";
////		}
////		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
////		nf.setGroupingUsed(false); 
////		nf.setMinimumFractionDigits(3);
////		
////		//最低提现金额
////		double min = this.epayLinksBankService.getMin();
////		double fee = this.epayLinksBankService.getFee();
////		if (min > 0 && tsAccount.getBalance() < min) {
////			this.request.setAttribute("minBalance", nf.format(tsAccount.getBalance() - fee) );//可提现金额
////		}else{
////			this.request.setAttribute("minBalance", nf.format(tsAccount.getBalance())) ;//可提现金额
////		}
////		this.request.setAttribute("min",min);//银企互联提现最低金额(0不检查)
////		this.request.setAttribute("fee",fee);//银企互联低于最低金额时手续费
////		
////		request.setAttribute("balance",nf.format(tsAccount.getBalance()));
////		
////		//获取结算银行卡号
////		BanksAccount banksAccount = null;
////		try{
////			banksAccount = this.banksService.getBankAccount();
////		}catch(Exception e){
////			this.saveActionError("请先绑定提现银行卡。只能有1个默认的提现账户。");
////			return "/shop/withdraw.jsp";
////		}
////		if (banksAccount == null) {
////			this.saveActionError("没有绑定提现银行卡号，请先绑定提现银行卡。");
////			return "/shop/withdraw.jsp";
////		}
////		banksAccount.setBankNumber(EncryptUtil.getBankNumber(banksAccount.getBankNumber()));//转换*号
////		
////		
////		this.request.setAttribute("banksAccount", banksAccount);
////		return "/shop/withdraw.jsp";
//		JSONObject jsonObject=new JSONObject();
//		
//		try {
////			this.checkRight(AUTH_SHOP_MY_ACCOUNT_SETTLE);//检查权限
//			//获取账户余额
//			String userId=this.getSessionUser().getUserId();
//			JSONObject jsonAccountObject=accountPayService.querySettleAmount("", userId,Constants.TS_ACCOUNT_OWNER_TYPE.USER);
//			String code=jsonAccountObject.getString("code");
//			if(code.equals("0")){
//				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//				jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有找到商家支付账户。");
//				return this.writeJson(jsonObject);
//			}
//			String amount=jsonAccountObject.getString("amount");
//			Double amountDouble=new Double(amount);
//			
//			String amountCount=jsonAccountObject.getString("balance");
//			Double settleAmount=new Double(amountCount);
//			
//			java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
//			nf.setGroupingUsed(false); 
//			nf.setMinimumFractionDigits(2);
//			
//			//最低提现金额
//			double min = cn.jekun.component.core.service.AccountService.getMin();
//			double fee = cn.jekun.component.core.service.AccountService.getFee();
//			int maxTimes = cn.jekun.component.core.service.AccountService.getMaxTimes();
//			if (min > 0 && settleAmount < min) {
//				jsonObject.put("minBalance",settleAmount==null?"0.00":nf.format((settleAmount - fee)>0?(settleAmount - fee):0));//可提现金额
//			}else{
//				jsonObject.put("minBalance",settleAmount==null?"0.00":nf.format(settleAmount)) ;//可提现金额
//			}
//			
//			jsonObject.put("min",min) ;//银企互联提现最低金额(0不检查)
//			jsonObject.put("fee",fee) ;//银企互联低于最低金额时手续费
//			jsonObject.put("maxTimes", maxTimes);
//			jsonObject.put("balance",settleAmount==null?"0.00":nf.format(settleAmount)) ;
//			jsonObject.put("amount",nf.format(amountDouble));
//			//获取结算银行卡号
//			BanksAccount banksAccount = null;
//			try{
//				banksAccount = this.banksService.getBankAccount(userId);
//				
//				if(banksAccount!=null){
//					banksAccount.setBankNumber(EncryptUtil.getBankNumber(banksAccount.getBankNumber()));
//					jsonObject.put("bankNumber",banksAccount.getBankNumber()==null?"":banksAccount.getBankNumber());
//					jsonObject.put("bankName",banksAccount.getBankName()==null?"":banksAccount.getBankName());
//					jsonObject.put("accountId",banksAccount.getAccountId()==null?"":banksAccount.getAccountId());
//					jsonObject.put("accountName",banksAccount.getAccountName()==null?"":banksAccount.getAccountName());
//					jsonObject.put("bankNumber_sort",EncryptUtil.getBankNumber(banksAccount.getBankNumber()));//银行帐号转换*号
//					
//					//判断最多提现次数
//					if (maxTimes >0) {
//						int times = this.accountService.getSettleTimes(banksAccount.getAccountId());
//						if (times>=maxTimes) {
//							jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//							jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，每天最多提现"+maxTimes+"次，您今天已经提现了"+times+"次");
//							return this.writeJson(jsonObject);
//						}
//					}
//				}
//			}catch(Exception e){
//				
//				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//				jsonObject.put(ErrorMsg.KEY_MESSAGE,"请先绑定提现银行卡。只能有1个默认的提现账户。");
//				return this.writeJson(jsonObject);
//			}
//			if (banksAccount == null) {
//				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//				jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有绑定提现银行卡号，请先绑定提现银行卡。");
//				return this.writeJson(jsonObject);
//			}
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
//		} catch (BusinessException e) {
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
//		} catch (Exception e) {
//			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
//			e.printStackTrace();
//		}
//		return this.writeJson(jsonObject);
//	}
//	/**
//	 * 提现确认
//	 */
//	public String confirm(){
//		this.checkRight(AUTH_SHOP_MY_ACCOUNT_SETTLE);//检查权限
//		
//		String accountId = request.getParameter("accountId");//提现账户代码
//		String amountStr = request.getParameter("amount");//提现金额
//		if (StringUtils.isBlank(accountId)) {
//			this.saveActionError("请选择提现的银行账户");
//			return "/shop/Withdraw/withdraw.do";
//		}
//		//判断最多提现次数
//		int maxTimes = cn.jekun.component.core.service.AccountService.getMaxTimes();
//		if (maxTimes >0) {
//			int times = this.accountService.getSettleTimes(accountId);
//			if (times>=maxTimes) {
//				this.saveActionError("对不起，每天最多提现"+maxTimes+"次，您今天已经提现了"+times+"次");
//				return "/shop/Withdraw/withdraw.do";
//			}
//		}
//		//检查提现金额
//		if (StringUtils.isBlank(amountStr)) {
//			this.saveActionError("请输入提现的金额");
//			return "/shop/Withdraw/withdraw.do";
//		}
//		double amount = 0;
//		try {
//			amount = Double.parseDouble(amountStr);
//		} catch (Exception e) {
//			this.saveActionError("提现金额必须是有效的数字");
//			return "/shop/Withdraw/withdraw.do";
//		}
//		if(amount <=0){
//			this.saveActionError("提现金额必须大于0");
//			return "/shop/Withdraw/withdraw.do";
//		}
//		return "/shop/withdraw_confirm.jsp";
//		
//	}
//	/**
//	 * 提现申请
//	 * @return
//	 */
//	public String settleApply(){
////		this.checkRight(AUTH_SHOP_MY_ACCOUNT_SETTLE);//检查权限
////		String accountId = request.getParameter("accountId");//提现账户代码
////		String bankNumber = request.getParameter("bankNumber");//银行账户
////		String bankName = request.getParameter("bankName");//银行名称
////		String accountName = request.getParameter("accountName");//账户姓名
////		String amountStr = request.getParameter("amount");//提现金额
////		String paypwd = request.getParameter("paypwd");//支付密码
////		String _sign_key = request.getParameter("_sign_key");//加密
////		
////		if (StringUtils.isBlank(accountId)) {
////			this.saveActionError("对不起，请选择提现的银行账户");
////			return "/shop/Withdraw/confirm.do";
////		}
////		if (StringUtils.isBlank(paypwd)) {
////			this.saveActionError("对不起，请输入支付密码");
////			return "/shop/Withdraw/confirm.do";
////		}
////		//检查提现金额
////		if (StringUtils.isBlank(amountStr)) {
////			this.saveActionError("对不起，请输入提现的金额");
////			return "/shop/Withdraw/confirm.do";
////		}
////		double amount = 0;
////		try {
////			amount = Double.parseDouble(amountStr);
////		} catch (Exception e) {
////			this.saveActionError("对不起，提现金额必须是有效的数字");
////			return "/shop/Withdraw/confirm.do";
////		}
////		if(amount <=0){
////			this.saveActionError("对不起，提现金额必须大于0");
////			return "/shop/Withdraw/confirm.do";
////		}
////		if (StringUtils.isBlank(bankNumber)) {
////			this.saveActionError("对不起，非法银行账户");
////			return "/shop/Withdraw/confirm.do";
////		}
////		if (StringUtils.isBlank(bankName)) {
////			this.saveActionError("对不起，非法银行名称");
////			return "/shop/Withdraw/confirm.do";
////		}
////		if (StringUtils.isBlank(accountName)) {
////			this.saveActionError("对不起，非法账户姓名");
////			return "/shop/Withdraw/confirm.do";
////		}
////		String md5 = EncryptUtil.md5(accountId+amountStr+bankNumber+bankName+accountName+paypwd);
////		if (!md5.equals(_sign_key)) {
////			this.saveActionError("非法提交");
////			return "/shop/Withdraw/confirm.do";
////		}
////		//获取账户信息
////		Map map = new HashMap();
////		map.put("shopId", this.getSessionUser().getShopId());
////		TsAccount tsAccount = this.accountService.one(this.accountService.getStatementName("query"),map );
////		if(tsAccount == null){
////			this.saveActionError("对不起，没有找到商家支付账户。");
////			return "/shop/Withdraw/confirm.do";
////		}
////		
////		//验证支付密码
////		if(StringUtils.isBlank(paypwd) || !tsAccount.getPassword().equals(paypwd)){
////			this.saveActionError("对不起，支付密码不正确");
////			return "/shop/Withdraw/confirm.do";
////		}
////		//验证余额
////		if(tsAccount.getBalance() < amount){
////			this.saveActionError("对不起，余额不足");
////			return "/shop/Withdraw/confirm.do";
////		}
////		//最低提现金额
////		double min = this.epayLinksBankService.getMin();
////		double fee = this.epayLinksBankService.getFee();
////		if (min > 0 && amount < min && tsAccount.getBalance() < (min - fee) ) {//提现金额小于最低提现金额,并且余额小于最低提现金额(min-fee)
////			this.saveActionError("对不起，提现金额小于"+min+"元收取"+fee+"元手续费,目前您可提现金额是"+(tsAccount.getBalance() - fee )+"元");
////			return "/shop/Withdraw/confirm.do";
////		}
////		//提现申请
////		String settleApplyId = null;//提现申请代码
////		try{
////			settleApplyId = this.banksService.settleApply(this.getSessionUser().getUserId(),accountId, amount,fee,paypwd);
////			this.logService.addShopAuditLog(AUTH_SHOP_MY_ACCOUNT_SETTLE, "申请提现成功，提现申请代码:"+settleApplyId);//记录操作日志
////		}catch(Exception e){
////			this.saveActionError("增加提现申请发生错误:"+e.getMessage());
////			return "/shop/withdraw_result.jsp";
////		}
////		if(settleApplyId == null){
////			this.saveActionError("增加提现申请失败");
////			return "/shop/withdraw_result.jsp";
////		}
////
////		this.saveActionMessage("申请成功,请等待银行处理");
////		return "/shop/withdraw_result.jsp";
//		
//		
//		
//		
//		JSONObject jsonObject=new JSONObject();
//		
//		String accountId = request.getParameter("accountId");//提现账户代码
//		String bankNumber = request.getParameter("bankNumber");//银行账户
//		String bankName = request.getParameter("bankName");//银行名称
//		String accountName = request.getParameter("accountName");//账户姓名
//		String amountStr = request.getParameter("amount");//提现金额
//		String paypwd = request.getParameter("paypwd");//支付密码
//		String _sign_key = request.getParameter("_sign_key");//加密
//		
//		if (StringUtils.isBlank(accountId)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，请选择提现的银行账户");
//			return this.writeJson(jsonObject);
//		}
//		//判断最多提现次数
//		int maxTimes = cn.jekun.component.core.service.AccountService.getMaxTimes();
//		if (maxTimes >0) {
//			int times = this.accountService.getSettleTimes(accountId);
//			if (times>=maxTimes) {
//				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//				jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，每天最多提现"+maxTimes+"次，您今天已经提现了"+times+"次");
//				return this.writeJson(jsonObject);
//			}
//		}
//		if (StringUtils.isBlank(paypwd)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，请输入支付密码");
//			return this.writeJson(jsonObject);
//		}
//		//检查提现金额
//		if (StringUtils.isBlank(amountStr)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，请输入提现的金额");
//			return this.writeJson(jsonObject);
//		}
//		double amount = 0;
//		try {
//			amount = Double.parseDouble(amountStr);
//		} catch (Exception e) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，提现金额必须是有效的数字");
//			return this.writeJson(jsonObject);
//			
//		}
//		if(amount <=0){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，提现金额必须大于0");
//			return this.writeJson(jsonObject);
//		}
//		if (StringUtils.isBlank(bankNumber)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，非法银行账户");
//			return this.writeJson(jsonObject);
//		}
//		if (StringUtils.isBlank(bankName)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，非法银行名称");
//			return this.writeJson(jsonObject);
//		}
//		if (StringUtils.isBlank(accountName)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，非法账户姓名");
//			return this.writeJson(jsonObject);
//		}
//		String md5 = EncryptUtil.md5(accountId+amountStr+bankNumber+bankName+accountName+paypwd);
//		if (!md5.equals(_sign_key)) {
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"非法提交");
//			return this.writeJson(jsonObject);
//		}
//		
//		
//		
//		//获取账户信息
//		String userId=this.getSessionUser().getUserId();
//		
//		//获取可提现金额 
//		JSONObject jsonAccountObject=accountPayService.querySettleAmount("", userId,Constants.TS_ACCOUNT_OWNER_TYPE.USER);
//		String settleCode=jsonAccountObject.getString("code");
//		if(settleCode.equals("0")){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"没有找到商家支付账户。");
//			return this.writeJson(jsonObject);
//		}
//		String amountCount=jsonAccountObject.getString("amount");
//		Double settleAmount=new Double(amountCount);
//		
//		TsAccount tsAccount = this.accountService.one(userId);
//		if(tsAccount == null){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，没有找到商家支付账户。");
//			return this.writeJson(jsonObject);
//		}
//		
//		//验证支付密码
//		if(StringUtils.isBlank(paypwd) || !tsAccount.getPassword().equals(paypwd)){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，支付密码不正确");
//			return this.writeJson(jsonObject);
//		}
//		//验证余额
//		if(settleAmount < amount){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，余额不足");
//			return this.writeJson(jsonObject);
//		}
//		//最低提现金额
//		double min = cn.jekun.component.core.service.AccountService.getMin();
//		double fee = 0;
//		if (min > 0 && amount <= min) {//提现金额小于等于最小金额
//			fee = cn.jekun.component.core.service.AccountService.getFee();//手续费
//		}
//		if (min > 0 && amount < min && ( amount + fee ) > settleAmount ) {//提现金额小于最低提现金额,并且余额小于最低提现金额(min-fee)
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"对不起，提现金额小于"+min+"元收取"+fee+"元手续费,目前您可提现金额是"+(settleAmount - fee )+"元");
//			return this.writeJson(jsonObject);
//		}
//		//提现申请
//		String settleApplyId = null;//提现申请代码
//		try{
//			JSONObject jsonSettleApply=this.accountPayService.settleApply("", tsAccount.getAccountId(), amount+"", "",accountId, paypwd,Constants.SG_APPLY_TYPE.SHOP_WEB);
//			String code=jsonSettleApply.getString("code");
//			String message=jsonSettleApply.getString("message");
//			
//			if(code.equals("0")){
//				throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE,message);
//			}
//			settleApplyId=jsonSettleApply.getString("applyId");
//			
//			//settleApplyId = this.banksService.settleApply(this.getSessionUser().getUserId(),accountId, amount,fee,paypwd);
//			this.logService.addShopAuditLog(AUTH_SHOP_MY_ACCOUNT_SETTLE, "申请提现成功，提现申请代码:"+settleApplyId);//记录操作日志
//		}catch(Exception e){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"增加提现申请发生错误:"+e.getMessage());
//			return this.writeJson(jsonObject);
//		}
//		if(settleApplyId == null){
//			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
//			jsonObject.put(ErrorMsg.KEY_MESSAGE,"增加提现申请失败");
//			return this.writeJson(jsonObject);
//		}
//		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
//		jsonObject.put(ErrorMsg.KEY_MESSAGE,"申请成功,请等待银行处理");
//		
//		return this.writeJson(jsonObject);
//		
//	}
//	
//	/**
//	 * 提现记录
//	 * @return
//	 */
//	public String querySettle(){
//		
//		
//		JSONObject jsonObject=new JSONObject();
//		
//		
//		try {
//			
//			String beginMoney="";//request.getParameter("beginMoney");
//			String endMoney="";//request.getParameter("endMoney");
//			String beginDate="";//request.getParameter("beginDate");
//			String endDate="";//request.getParameter("endDate");
//			String bankName="";//request.getParameter("bankName");
//			
//			String pageSize = this.request.getParameter("pageSize");
//			String currentPage = this.request.getParameter("currentPage");
//			String userId =this.getSessionUser().getUserId();
////			formMap.put("shopId", shopId);
////			SqlUtis.convertBetweenValue(formMap, "commitTime");
////			SqlUtis.prepareSql(formMap
////					,SqlUtis.getSQL(formMap, "a."+SgSettleApply.COL_COMMIT_TIME,"commitTime", Types.DATE)
////					,SqlUtis.getSQL(formMap, "u."+RsUser.COL_REAL_NAME, realName, Types.VARCHAR,SqlUtis.LIKE)
////					,SqlUtis.getSQL(formMap, "u."+RsUser.COL_USER_NAME, userName, Types.VARCHAR,SqlUtis.LIKE)
////					);
//			jsonObject = this.banksService.listSettleApply(userId,beginDate,endDate,beginMoney,endMoney,bankName,pageSize, currentPage);
//			
//			
//			
//			
//			
//		} catch (BusinessException e) {
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
//		} catch (Exception e) {
//			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
//			e.printStackTrace();
//		}
//		
//		return this.writeJson(jsonObject);
//		
//		
//		
//	}
//}
